package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    private AddressBook addressBook;
    //declare instance variables to reference containers
    //passed as arguments from the main class
    
    //Address container is to be passed as parameter from the main controller
    public AddressCtr(/*---*/)
    {
        addressBook = new AddressBook();
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    
    /**
     * Find a person by his id
     */
    public Person getPerson(int id)
    {
        return addressBook.getPerson(id);
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
    }
    
    /**
     * Create a person
     */
    public void createPerson(int id, String name, String address, String postalCode, String city, String phone)
    {  
       boolean found = false;
       while (found == false){
           if (getPerson(id) == null || getPerson(id).getId() != id){
               Person person = new Person(id, name, address, postalCode, city, phone);
               addressBook.addPerson(person);
               found = true;
            }
            else {
          found = true;
        }
    }
    }
    
    /**
     * Delete a person by his id
     */
    public void deletePerson(int id)
    {
        addressBook.deletePerson(id);
    }
    
    /**
     * update person's stats
     */
    public void updatePerson(int id, String name, String address, String postalCode, String city, String phone)
    {
        addressBook.updatePerson(id, name, address, postalCode, city, phone);
    }
    
    public ArrayList listAllPersons()
    {
          return addressBook.listAllPersons();
    }
}
